<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="departments" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>

			<el-table-column prop="name" label="明细名称" min-width="111" sortable>
			</el-table-column>

			<el-table-column prop="types.name" label="类型" width="180" sortable>
			</el-table-column>

            <!--注意者是对象，所以要点他下面的name-->


			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :current-Page="currentPage"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--
		:rules="  表单校验规则
		prop="   绑定校验规则名称
		ref="saveForm"  取别名，下面获取到他，进行主动校验
		-->

		<!--新增/修改界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">

				<el-form-item label="明细名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="分类">
					<el-select v-model="saveForm.types" clearable value-key="id" placeholder="请选择">

						<el-option
								v-for="item in type"
								:key="item.id"
								:label="item.name"
								:value="item">
							<span style="float: left">{{ item.name }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, saveUser } from '../../api/api';

	export default {
		data() {
			return {
                title:"",
				filters: {
                    keyword: ''
				},

                /*修改为我们的名字*/
				departments: [],
				type:[],

				total: 0,
                currentPage: 1,
                pageSize:10,

				listLoading: false,
				sels: [],//列表选中列

				saveFormVisible: false,//新增界面是否显示
				saveLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
                    id: [
                        { required: true, message: '请输入编号', trigger: 'blur' }
                    ]
				},
				//新增界面数据
				saveForm: {
				    id:null,
					name: "",
                    types_id:null,


				}

			}
		},
		methods: {
            search(){
                //跳到第一页
                this.currentPage = 1;
				this.getDepartment();
			},
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getDepartment();
            },
			//获取部门列表
			getDepartment() {
				let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                    keyword: this.filters.keyword
				};
				//开启忙等框
				this.listLoading = true;
				//NProgress.start();
				console.log(para)
				//发送axios    请求参数上面的para
				this.$http.post("/detailType/page",para)
					//取值
					.then(result => {
					    //关闭忙等框
                        this.listLoading = false;
					   console.log(result);
					   result = result.data;//获取全部的数据
						this.total = result.total;
					   this.departments = result.rows;//数据中的rows
					})
					//报错
					.catch(result=>{
						console.log(result);
						//ElementUI 的弹框
                        this.$message({
                            type: 'error',
                            message: '系统错误,我们正在抢救程序员!!'
                        });
					})

				/*getUserListPage(para).then((res) => {
					this.total = res.data.total;
					this.departments = res.data.departments;
					//关闭忙等
					this.listLoading = false;
					//NProgress.done();
				});*/
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					//将id携带过去
					console.log(row.id);
					this.$http.delete("/detailType/"+row.id)
						.then(result=>{
                            //关闭忙等框
						    this.listLoading = false;
                            result = result.data;
                            //判断
                            if(result.success){
                                //弹框提示
                                this.$message({
                                    type: 'success',
                                    message: result.message
                                });
                                //跳到第一页，防止页面没数据
                                this.currentPage = 1;
                                //刷新页面
								this.getDepartment();

							}else{
								//弹框提示
                                this.$message({
                                    type: 'error',
                                    message: result.message
                                });
                                //跳到第一页
                                this.currentPage = 1;
                                //刷新页面
                                this.getDepartment();

							}
						})
						.catch(result=>{
                            this.$message({
                                type: 'error',
                                message: '系统错误,我们正在抢救程序员!!'
                            });
						})

				}).catch(() => {
					//取消就没有了，then才是判断删除与否

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
                this.title = "编辑";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
				console.log("修改row："+row.dirPath);
				//判断dirParts是否有值
				if(row.dirPath){
				    //以"/"分割
					let array = row.dirPath.split("/");
					console.log(array);
					let tmp = [];
					//循环获取中间的数据
					for(let i=1;i<array.length-1;i++){
						tmp.push(parseInt(array[i]));
					}
					console.log("tmp: "+tmp);
					//再将tmp还回给表单parent
					this.saveForm.parent = tmp;
				}else {
				    console.log("结束了")
				}
			},
			//显示新增界面
			handleAdd: function () {
				this.title = "新增";
				this.saveFormVisible = true;
				this.saveForm = {
					id:null,
					name: "",
                    types_id:null,
                    types: null

				};
			},
			//编辑an修改
			saveSubmit: function () {
                //validate主动开启校验
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.saveForm);
                            //调整格式
							if(para.parent){
							    para.parent={"id":para.parent[para.parent.length-1]}

							}
							//console.log("区分"+para);
							this.$http.post("/detailType/addOnUpdate",para)
								.then(result=>{
                                    result = result.data;
                                    //关闭模态框
                                    this.saveLoading = false;
                                    if(result.success){
                                        //弹框提示
                                        this.$message({
                                            type: 'success',
                                            message: result.message
                                        });
                                        //跳到第一页，防止页面没数据
                                        this.currentPage = 1;
                                        //刷新页面
                                        this.getDepartment();
                                        this.$refs['saveForm'].resetFields();//重置表单校验
                                        this.saveFormVisible = false;//关闭模态框

                                    }else{
                                        //弹框提示
                                        this.$message({
                                            type: 'error',
                                            message: result.message
                                        });
                                        //跳到第一页
                                        this.currentPage = 1;
                                        //刷新页面
                                        this.getDepartment();

                                    }
								})
								.catch(result=>{
								    //关闭忙等
                                    this.saveLoading = false;
                                    this.$message({
                                        type: 'error',
                                        message: "业务繁忙，我们正在抢救程序员！！！"
                                    });
								})
						});
					}
				});
			},

			//复选框，sels：勾选的全部数据，
			selsChange: function (sels) {
                //赋值到上面的sels数组中
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)//.toString();  将toString注掉，他本来就是个数组
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					console.log(ids);
					//NProgress.start();
					//let para = { ids: ids };   本来就是个对象多此一举
					this.$http.patch("/detailType",ids)
						.then(result=>{
                            //关闭忙等框
                            this.listLoading = false;
                            result = result.data;//AjaxRxxxx
                            //判断成功与否
							if(result.success){
                                this.$message({
                                    type: 'success',
                                    message: result.message
                                });
                                //跳到第一页
                                this.currentPage = 1;
                                //刷新页面
                                this.getDepartment();

                            }else{
                                this.$message({
                                    type: 'error',
                                    message: result.message
                                });
                                //跳到第一页
                                this.currentPage = 1;
                                //刷新页面
                                this.getDepartment();

                            }
						})
						.catch(result=>{
                            this.$message({
                                type: 'error',
                                message: '系统错误,我们正在抢救程序员!!'
                            });
						})
				}).catch(() => {
					//取消
				});
			},
            getType(){
                this.$http.get("/detail/list")
					.then(result=>{
					console.log(result);
                        console.log("type.date: "+result.data);
					    this.type = result.data;

					})
			},

		},
		mounted() {
			this.getDepartment();
			this.getType();
		}
	}

</script>

<style scoped>

</style>